package com.riddle.admin.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.riddle.model.Role;
import com.riddle.model.User;
import com.riddle.service.BaseService;
import com.riddle.service.UserService;
import com.riddle.util.JsonUtil;
import com.riddle.util.StringUtil;
import com.riddle.vmodel.QueryModel;
import com.riddle.vmodel.VMsg;
import com.riddle.vmodel.ValuePair;

@Controller
@RequestMapping("/admin/user")
public class UserController extends BaseController<User> {

	@RequestMapping()
	public String index() {
		return "admin/user/index";
	}
	
	@Resource
	private UserService userService;
	
	protected BaseService getService() {
		return userService;
	}
	
	@RequestMapping(value="/list", produces="application/json;charset=utf-8")
	@ResponseBody
	public String list(QueryModel queryModel) {
		return super.list(queryModel);
	}
	
	@RequestMapping("/add")
	public String add() {
		return "admin/user/add";
	}
	
	@RequestMapping(value="/addmodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String addModel(User user) {
		if(user != null && !StringUtil.isNullOrEmpty(user.getPassword())) {
			user.setPassword(new Md5Hash(user.getPassword()).toString());
		}
		return super.addModel(user);
	}
	
	@RequestMapping("/modify")
	public ModelAndView modify(Long id) {
		User user = userService.query(id);
		return new ModelAndView("admin/user/modify", "user", user);
	}
	
	@RequestMapping(value="/modifymodel", produces="application/json;charset=utf-8")
	@ResponseBody
	public String modifyModel(User user) {
		if(user != null && !StringUtil.isNullOrEmpty(user.getPassword())) {
			user.setPassword(new Md5Hash(user.getPassword()).toString());
		}
		return super.modifyModel(user);
	}
	
	@RequestMapping(value="/delete", produces="application/json;charset=utf-8")
	@ResponseBody
	public String delete(Long id) {
		return super.delete(id);
	}
	
	public String getParentModel() {
		return null;
	}
	
	@RequestMapping("/assign")
	public ModelAndView assign(Long id) {
		User user = userService.getUserWithRole(id);
		user.getRolesStr();
		return new ModelAndView("admin/user/assign", "user", user);
	}
	
	@RequestMapping(value="/assignrole", produces="application/json;charset=utf-8")
	@ResponseBody
	public String assignPermission(Long id, String roles) {
		List<ValuePair<Long, String>> list = new ArrayList<ValuePair<Long, String>>();
		String[] strs = roles.split(",");
		for(String s : strs) {
			ValuePair<Long, String> value = new ValuePair<Long, String>();
			value.setValue1(id);
			value.setValue2(s);
			list.add(value);
		}
		userService.deleteUserRole(id);
		userService.saveUserRole(list);
		VMsg msg = new VMsg();
		msg.setMessage("success");
		return JsonUtil.toJson(msg);
	}
}


